#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include "os_api.h"
#include "atclient.h"

#define MAT_CMD_TYPE_MIN	_MAT_MIN

#define MAT_plusTESTVALUE	MAT_CMD_TYPE_MIN+1
#define MAT_plusTESTSTRING	MAT_CMD_TYPE_MIN+2

#define MAT_CMD_TYPE_MAX	_MAT_MAX


static utlAtParameter_T one_decimal_params[] = { 
	utlDEFINE_DECIMAL_AT_PARAMETER( utlAT_PARAMETER_ACCESS_READ_WRITE, utlAT_PARAMETER_PRESENCE_OPTIONAL)
	};

static utlAtParameter_T one_string_params[] = {
	utlDEFINE_STRING_AT_PARAMETER( utlAT_PARAMETER_ACCESS_READ_WRITE, utlAT_PARAMETER_PRESENCE_OPTIONAL),
	};

static utlAtParameter_T two_string_params[] = {
	utlDEFINE_STRING_AT_PARAMETER( utlAT_PARAMETER_ACCESS_READ_WRITE, utlAT_PARAMETER_PRESENCE_OPTIONAL),
	utlDEFINE_STRING_AT_PARAMETER( utlAT_PARAMETER_ACCESS_READ_WRITE, utlAT_PARAMETER_PRESENCE_OPTIONAL),
	};

static utlAtParameter_T d_str_d_params[] = {
	utlDEFINE_DECIMAL_AT_PARAMETER( utlAT_PARAMETER_ACCESS_READ_WRITE, utlAT_PARAMETER_PRESENCE_OPTIONAL),
	utlDEFINE_STRING_AT_PARAMETER( utlAT_PARAMETER_ACCESS_READ_WRITE, utlAT_PARAMETER_PRESENCE_OPTIONAL),
	utlDEFINE_DECIMAL_AT_PARAMETER( utlAT_PARAMETER_ACCESS_READ_WRITE, utlAT_PARAMETER_PRESENCE_OPTIONAL),	
	};	



RETURNCODE_T  AtTESTVALUE(            const utlAtParameterOp_T        op,
		const char                      *command_name_p,
		const utlAtParameterValue_P2c   parameter_values_p,
		const size_t                    num_parameters,
		const char                      *info_text_p,
		unsigned int                    *xid_p,
		void                            *arg_p)
{
	UINT32 sAtpIndex = * (UINT32 *) arg_p;
	UINT32 atHandle = atcmds_make_at_handle( sAtpIndex );
	RETURNCODE_T ret = utlSUCCESS;
	
	static int Value = 0;	

	char buf[32] = {0};
	
	printf("%s: atHandle = %d, sAtpIndex = %d.\n", __FUNCTION__, atHandle, sAtpIndex);

	*xid_p = atHandle;
	switch(op)
	{	
		case TEL_EXT_SET_CMD:
			if(getExtValue(parameter_values_p, 0, (int *)&Value, 0, 1, 0 ) == TRUE)
			{				
                sprintf(buf, "+TESTVALUE: %d", Value);
				ret = ATRESP(atHandle, ATCI_RESULT_CODE_OK, 0, buf);						
			}
			else
			{
				ret = ATRESP(atHandle, ATCI_RESULT_CODE_ERROR, 0, NULL);		
			}
			break;

			
		case TEL_EXT_GET_CMD:
            sprintf(buf, "+TESTVALUE: %d", Value);
			ret = ATRESP( atHandle,ATCI_RESULT_CODE_OK,0,buf);	
			break;
		case TEL_EXT_TEST_CMD:
			ret = ATRESP(atHandle, ATCI_RESULT_CODE_OK, 0, NULL);						
			break;

		default:
			ret = ATRESP(atHandle, ATCI_RESULT_CODE_ERROR, 0, NULL);
			break;
	}
	
	return ret;
}


RETURNCODE_T  AtTESTSTRING(            const utlAtParameterOp_T        op,
		const char                      *command_name_p,
		const utlAtParameterValue_P2c   parameter_values_p,
		const size_t                    num_parameters,
		const char                      *info_text_p,
		unsigned int                    *xid_p,
		void                            *arg_p)
{
	UINT32 sAtpIndex = * (UINT32 *) arg_p;
	UINT32 atHandle = atcmds_make_at_handle( sAtpIndex );
	RETURNCODE_T ret = utlSUCCESS;

	char buf[64] = {0};

	static char buffer[32] = {0};
	static int bufferlen = 0;
	
	printf("%s: atHandle = %d, sAtpIndex = %d.\n", __FUNCTION__, atHandle, sAtpIndex);

	*xid_p = atHandle;
	switch(op)
	{	
		case TEL_EXT_SET_CMD:
			if (getExtString(parameter_values_p, 0, buffer, sizeof(buffer)-1, (INT16 *)&bufferlen, NULL) == TRUE)
			{				
                printf("%s: NULL = %s.\n", __FUNCTION__, buffer);
				ret = ATRESP(atHandle, ATCI_RESULT_CODE_OK, 0, NULL);						
			}
			else
			{
				ret = ATRESP(atHandle, ATCI_RESULT_CODE_ERROR, 0, NULL);		
			}
			break;

			
		case TEL_EXT_GET_CMD:
            sprintf(buf, "+TESTSTRING: %s", buffer);
			ret = ATRESP( atHandle,ATCI_RESULT_CODE_OK,0,buf);	
			break;
		case TEL_EXT_TEST_CMD:
			ret = ATRESP(atHandle, ATCI_RESULT_CODE_OK, 0, NULL);						
			break;

		default:
			ret = ATRESP(atHandle, ATCI_RESULT_CODE_ERROR, 0, NULL);
			break;
	}
	
	return ret;
}


static utlAtCommand_T ext_at_commands[] = {
	utlDEFINE_EXTENDED_EXACTION_VSYNTAX_AT_COMMAND(MAT_plusTESTVALUE, "+TESTVALUE", one_decimal_params, AtTESTVALUE, AtTESTVALUE, AtTESTVALUE),
	utlDEFINE_EXTENDED_EXACTION_VSYNTAX_AT_COMMAND(MAT_plusTESTSTRING, "+TESTSTRING", one_string_params, AtTESTSTRING, AtTESTSTRING, AtTESTSTRING),
};


#define utlNumberOf(n) (sizeof(n) / sizeof((n)[0]))

unsigned int ext_at_commands_num = utlNumberOf(ext_at_commands);


int main(void)
{
    int i;
    for (i = 0; i < ext_at_commands_num; i++){
        //printf("register atcmd: %s, sizeof: %d\r\n", ext_at_commands[i].name_p, sizeof(utlAtCommand_T));
    }    

    atcmds_register(ext_at_commands, ext_at_commands_num);

	return 0;
}
